package forkjoin;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import entity.hard.Education;
import entity.hard.Student;
import entity.hard.Teacher;
import entity.simple.SimpleEducation;
import entity.simple.SimpleStudent;
import entity.simple.SimpleTeacher;
import junit.RepeatRule;
import org.junit.Rule;

public class MockData {


  private static final List<SimpleEducation> SIMPLE_EDUCATIONS = Arrays
      .asList(SimpleEducation.values());
  private static final List<Education> EDUCATIONS = Arrays.asList(Education.values());
  private static final int size = 6;
  @Rule
  public RepeatRule repeatRule = new RepeatRule();

  protected static List<Teacher> data(int teacherSize, int perTeacherStudentSize,
                                      boolean nullAble) {
    List<Teacher> teachers = new ArrayList<>(teacherSize);
    for (int i = 1; i <= teacherSize; i++) {
      Teacher teacher = new Teacher(name(i, nullAble), course(i, nullAble), salary(i, nullAble),
          married(i, nullAble),
          education(i, nullAble));
      List<Student> students = new ArrayList<>(perTeacherStudentSize);
      for (int j = 1; j <= perTeacherStudentSize; j++) {
        students.add(
            new Student(id(i, nullAble), name(i, nullAble), sex(i, nullAble), birthday(i, nullAble),
                salary(i, nullAble))
        );
      }
      teacher.setStudents(students);
      teachers.add(teacher);
    }
    return teachers;
  }

  protected static List<SimpleTeacher> simpleData(int teacherSize, int perTeacherStudentSize,
                                                  boolean nullAble) {
    List<SimpleTeacher> simpleTeachers = new ArrayList<>(teacherSize);
    for (int i = 1; i <= teacherSize; i++) {
      SimpleTeacher simpleTeacher = new SimpleTeacher(name(i, nullAble), course(i, nullAble),
          salary(i, nullAble),
          married(i, nullAble),
          simpleEducation(i, nullAble));
      List<SimpleStudent> SimpleStudents = simpleStudents(perTeacherStudentSize, i, nullAble);
      simpleTeacher.setSimpleStudents(SimpleStudents);
      simpleTeachers.add(simpleTeacher);
    }
    return simpleTeachers;
  }

  protected static List<SimpleStudent> simpleStudents(int perTeacherStudentSize, int i,
      boolean nullAble) {
    List<SimpleStudent> SimpleStudents = new ArrayList<>(perTeacherStudentSize);
    for (int j = 1; j <= perTeacherStudentSize; j++) {
      SimpleStudents.add(
          new SimpleStudent(id(i, nullAble), name(i, nullAble), sex(i, nullAble),
              RandomUtils.nextBoolean(), new Date(),
              salary(i, nullAble))
      );
    }
    return SimpleStudents;
  }

  private static Boolean married(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return RandomUtils.nextBoolean();
  }

  private static Date birthday(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return RandomUtils.randomDate();
  }

  private static Integer id(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return RandomUtils.nextInt();
  }

  private static SimpleEducation simpleEducation(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return SIMPLE_EDUCATIONS.get(RandomUtils.nextInt(size));
  }

  private static Education education(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return EDUCATIONS.get(RandomUtils.nextInt(size));
  }

  private static String sex(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return RandomUtils.randomStrFrom("男", "女");
  }


  private static Double salary(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return RandomUtils.nextDouble(0, 10000.00000);
  }

  private static String course(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return RandomUtils.randomStrFrom("数学", "语文", "英语");
  }

  private static String name(int i, boolean nullAble) {
    if (nullAble && i % randomInt() == 0) {
      return null;
    }
    return RandomUtils.randomChinese(RandomUtils.nextInt(2, 4));
  }

  private static int randomInt() {
    return RandomUtils.nextInt(2, 5);
  }
}
